package com.r2coding.test;

import com.r2coding.domain.strategy.service.armory.IStrategyArmory;
import com.r2coding.domain.strategy.service.armory.IStrategyDispatch;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 抽奖奖品Dao 单元测试类
 *
 * @author caolx
 * @date 2025/7/22 23:19
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AwardArmoryTest {

    @Resource
    private IStrategyArmory strategyArmory;
    @Resource
    private IStrategyDispatch strategyDispatch;

    @Test
    public void test_strategyArmory() {
        boolean success = strategyArmory.assembleLotteryStrategy(100001L);
        log.info("测试结果: {}", success);
    }

    @Test
    public void test_getRandomAwardId() {
        Map<Integer, Integer> resultMap = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            Integer randomAwardId = strategyDispatch.getRandomAwardId(100001L);
            resultMap.compute(randomAwardId, (k, v) -> v == null ? 1 : v + 1);
        }
        for (Map.Entry<Integer, Integer> entry : resultMap.entrySet()) {
            log.info("测试结果，奖品ID： {}, 中奖次数: {}", entry.getKey(), entry.getValue());
        }
    }

    /**
     * 根据策略ID+权重值，从装配的策略中随机获取奖品ID值
     */
    @Test
    public void test_getRandomAwardId_ruleWeightValue() {
        log.info("测试结果：{} - 4000 策略配置", strategyDispatch.getRuleWeightRandomAwardId(100001L, 4000));
        log.info("测试结果：{} - 5000 策略配置", strategyDispatch.getRuleWeightRandomAwardId(100001L, 5000));
        log.info("测试结果：{} - 6000 策略配置", strategyDispatch.getRuleWeightRandomAwardId(100001L, 6000));
    }
}
